import tornado.web
import asyncio
import random
from core.base_tornado_request_handler import BaseTornadoRequestHandler
from role_module.role_controller import RoleController

class CraftHandler(BaseTornadoRequestHandler):
  def initialize(self, **kwargs):
    super().initialize(**kwargs)
    self.role_index = self.application.settings.get("role_index", 0)
    self.role_list = ["a", "b", "c", "d"]
    
  async def get(self, action):
    print(self.task_list)
    if action == "start":
      await self.start_task()
    elif action == "stop":
      await self.stop_task()
    self.write(f"baku, {action}")
    
  async def start_task(self):
    self.stop_all_task()
    print("start_task")
    self.task_list.append(asyncio.create_task(self.task_cor()))
    
  async def task_cor(self):
    while self.role_index < len(self.role_list):
      await RoleController(self.role_list[self.role_index]).start()
      self.role_index += 1
    
  async def stop_task(self):
    print("stop_task")
    await self.stop_all_task()
      